//
//  WWVisitorView.swift
//  -00 旺旺微博
//
//  Created by 伍良旺 on 16/4/16.
//  Copyright © 2016年 伍良旺. All rights reserved.
//

import UIKit
import SnapKit
class WWVisitorView: UIView {
    
    //按钮点击的回调  在visitorController中赋值
    var navItemClickAction:(() -> ())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    //跳转控制器 更改控件的值的接口
    func setVisitorViewSubView(imageName:String?,title:String?){
        
        //如果有值就重新赋值
        if let img = imageName, tit = title{
            houseImage.image = UIImage(named: img)
            
            promptLabel.text = tit
            
            print(tit)
            
            backImage.hidden = true
        }else{ // 没值时就直接使用 说明是主页  调用一下圈圈转动的方法
            turnBackImage()
        }
    }
    
    func turnBackImage(){
        //旋转动画
        let anmi = CABasicAnimation(keyPath: "transform.rotation")
        //时长
        anmi.duration = 15
        //旋转的角度
        anmi.toValue = 2 * M_PI
        //执行次数
        anmi.repeatCount = MAXFLOAT
        //当tabBar切换时 或者 回到后台时  动画是会停止的  需要手动关闭
        anmi.removedOnCompletion = false
        //添加动画
        backImage.layer.addAnimation(anmi, forKey: "")
        
    }
    
    func setupUI(){
        //背景颜色 自定义的方法
        backgroundColor = RGB(red: 237, green: 237, blue: 237)
        //添加控件
        addSubview(backImage)
        addSubview(coverImage)
        addSubview(houseImage)
        addSubview(promptLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
      //  turnBackImage()

    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //约束设置子控件的位置
        
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        coverImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        houseImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
                make.centerY.equalTo(self.snp_centerY)
        }
        
        promptLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(coverImage.snp_bottom).offset(16)
            make.centerX.equalTo(coverImage)
            make.height.equalTo(36)
            make.width.equalTo(230)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(promptLabel)
            make.top.equalTo(promptLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(promptLabel)
            make.top.equalTo(promptLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
    }
    
    
    //懒加载控件
    //背后的小圈圈
    private lazy var backImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var coverImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var houseImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文字
    private lazy var promptLabel:UILabel = {
        let la = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        la.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"

        la.numberOfLines = 0
        
        return la
    }()
    
   //登录按钮
    private lazy var registerBtn:UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickNavItem", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    private lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
         btn.addTarget(self, action: "clickNavItem", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //按钮点击事件  私有事件  点击时调用全局闭包属性
    @objc private func clickNavItem(){
        navItemClickAction?()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
